home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_002 / microemacs / word.c < prev   
C/C++ Source or Header  |  1992-05-06  |  9KB  |  274 lines

  1. /*
  2.  * The routines in this file implement commands that work word at a time.
  3.  * There are all sorts of word mode commands. If I do any sentence and/or
  4.  * paragraph mode commands, they are likely to be put in this file.
  5.  */
  6.  
  7. #include        <stdio.h>
  8. #include        "ed.h"
  9.  
  10.  
  11. /* Word wrap on n-spaces. Back-over whatever precedes the point on the current
  12.  * line and stop on the first word-break or the beginning of the line. If we
  13.  * reach the beginning of the line, jump back to the end of the word and start
  14.  * a new line.  Otherwise, break the line at the word-break, eat it, and jump
  15.  * back to the end of the word.
  16.  *      NOTE:  This function may leaving trailing blanks.
  17.  * Returns TRUE on success, FALSE on errors.
  18.  */
  19. wrapword(n)
  20. int n;
  21. {
  22.         register int cnt, oldp;
  23.         oldp = curwp->w_dotp;
  24.         cnt = -1;
  25.         do {                            
  26.                 cnt++;
  27.                 if (! backchar(NULL, 1))
  28.                         return(FALSE);
  29.         }
  30.         while (! inword());
  31.         if (! backword(NULL, 1))
  32.                 return(FALSE);
  33.         if (oldp == (int) (curwp->w_dotp && curwp->w_doto)) {
  34.                 if (! backdel(NULL, 1))
  35.                         return(FALSE);
  36.                 if (! newline(NULL, 1))
  37.                         return(FALSE);
  38.         }
  39.         return(forwword(NULL, 1) && forwchar(NULL, cnt));
  40. }
  41.                                 
  42. /*
  43.  * Move the cursor backward by "n" words. All of the details of motion are
  44.  * performed by the "backchar" and "forwchar" routines. Error if you try to
  45.  * move beyond the buffers.
  46.  */
  47. backword(f, n)
  48. {
  49.         if (n < 0)
  50.                 return (forwword(f, -n));
  51.         if (backchar(FALSE, 1) == FALSE)
  52.                 return (FALSE);
  53.         while (n--) {
  54.                 while (inword() == FALSE) {
  55.                         if (backchar(FALSE, 1) == FALSE)
  56.                                 return (FALSE);
  57.                 }
  58.                 while (inword() != FALSE) {
  59.                         if (backchar(FALSE, 1) == FALSE)
  60.                                 return (FALSE);
  61.                 }
  62.         }
  63.         return (forwchar(FALSE, 1));
  64. }
  65.  
  66. /*
  67.  * Move the cursor forward by the specified number of words. All of the motion
  68.  * is done by "forwchar". Error if you try and move beyond the buffer's end.
  69.  */
  70. forwword(f, n)
  71. {
  72.         if (n < 0)
  73.                 return (backword(f, -n));
  74.         while (n--) {
  75.                 while (inword() == FALSE) {
  76.                         if (forwchar(FALSE, 1) == FALSE)
  77.                                 return (FALSE);
  78.                 }
  79.                 while (inword() != FALSE) {
  80.                         if (forwchar(FALSE, 1) == FALSE)
  81.                                 return (FALSE);
  82.                 }
  83.         }
  84.         return (TRUE);
  85. }
  86.  
  87. /*
  88.  * Move the cursor forward by the specified number of words. As you move,
  89.  * convert any characters to upper case. Error if you try and move beyond the
  90.  * end of the buffer. Bound to "M-U".
  91.  */
  92. upperword(f, n)
  93. {
  94.         register int    c;
  95.  
  96.         if (n < 0)
  97.                 return (FALSE);
  98.         while (n--) {
  99.                 while (inword() == FALSE) {
  100.                         if (forwchar(FALSE, 1) == FALSE)
  101.                                 return (FALSE);
  102.                 }
  103.                 while (inword() != FALSE) {
  104.                         c = lgetc(curwp->w_dotp, curwp->w_doto);
  105.                         if (c>='a' && c<='z') {
  106.                                 c -= 'a'-'A';
  107.                                 lputc(curwp->w_dotp, curwp->w_doto, c);
  108.                                 lchange(WFHARD);
  109.                         }
  110.                         if (forwchar(FALSE, 1) == FALSE)
  111.                                 return (FALSE);
  112.                 }
  113.         }
  114.         return (TRUE);
  115. }
  116.  
  117. /*
  118.  * Move the cursor forward by the specified number of words. As you move
  119.  * convert characters to lower case. Error if you try and move over the end of
  120.  * the buffer. Bound to "M-L".
  121.  */
  122. lowerword(f, n)
  123. {
  124.         register int    c;
  125.  
  126.         if (n < 0)
  127.                 return (FALSE);
  128.         while (n--) {
  129.                 while (inword() == FALSE) {
  130.                         if (forwchar(FALSE, 1) == FALSE)
  131.                                 return (FALSE);
  132.                 }
  133.                 while (inword() != FALSE) {
  134.                         c = lgetc(curwp->w_dotp, curwp->w_doto);
  135.                         if (c>='A' && c<='Z') {
  136.                                 c += 'a'-'A';
  137.                                 lputc(curwp->w_dotp, curwp->w_doto, c);
  138.                                 lchange(WFHARD);
  139.                         }
  140.                         if (forwchar(FALSE, 1) == FALSE)
  141.                                 return (FALSE);
  142.                 }
  143.         }
  144.         return (TRUE);
  145. }
  146.  
  147. /*
  148.  * Move the cursor forward by the specified number of words. As you move
  149.  * convert the first character of the word to upper case, and subsequent
  150.  * characters to lower case. Error if you try and move past the end of the
  151.  * buffer. Bound to "M-C".
  152.  */
  153. capword(f, n)
  154. {
  155.         register int    c;
  156.  
  157.         if (n < 0)
  158.                 return (FALSE);
  159.         while (n--) {
  160.                 while (inword() == FALSE) {
  161.                         if (forwchar(FALSE, 1) == FALSE)
  162.                                 return (FALSE);
  163.                 }
  164.                 if (inword() != FALSE) {
  165.                         c = lgetc(curwp->w_dotp, curwp->w_doto);
  166.                         if (c>='a' && c<='z') {
  167.                                 c -= 'a'-'A';
  168.                                 lputc(curwp->w_dotp, curwp->w_doto, c);
  169.                                 lchange(WFHARD);
  170.                         }
  171.                         if (forwchar(FALSE, 1) == FALSE)
  172.                                 return (FALSE);
  173.                         while (inword() != FALSE) {
  174.                                 c = lgetc(curwp->w_dotp, curwp->w_doto);
  175.                                 if (c>='A' && c<='Z') {
  176.                                         c += 'a'-'A';
  177.                                         lputc(curwp->w_dotp, curwp->w_doto, c);
  178.                                         lchange(WFHARD);
  179.                                 }
  180.                                 if (forwchar(FALSE, 1) == FALSE)
  181.                                         return (FALSE);
  182.                         }
  183.                 }
  184.         }
  185.         return (TRUE);
  186. }
  187.  
  188. /*
  189.  * Kill forward by "n" words. Remember the location of dot. Move forward by
  190.  * the right number of words. Put dot back where it was and issue the kill
  191.  * command for the right number of characters. Bound to "M-D".
  192.  */
  193. delfword(f, n)
  194. {
  195.         register int    size;
  196.         register LINE   *dotp;
  197.         register int    doto;
  198.  
  199.         if (n < 0)
  200.                 return (FALSE);
  201.         dotp = curwp->w_dotp;
  202.         doto = curwp->w_doto;
  203.         size = 0;
  204.         while (n--) {
  205.                 while (inword() == FALSE) {
  206.                         if (forwchar(FALSE, 1) == FALSE)
  207.                                 return (FALSE);
  208.                         ++size;
  209.                 }
  210.                 while (inword() != FALSE) {
  211.                         if (forwchar(FALSE, 1) == FALSE)
  212.                                 return (FALSE);
  213.                         ++size;
  214.                 }
  215.         }
  216.         curwp->w_dotp = dotp;
  217.         curwp->w_doto = doto;
  218.         return (ldelete(size, TRUE));
  219. }
  220.  
  221. /*
  222.  * Kill backwards by "n" words. Move backwards by the desired number of words,
  223.  * counting the characters. When dot is finally moved to its resting place,
  224.  * fire off the kill command. Bound to "M-Rubout" and to "M-Backspace".
  225.  */
  226. delbword(f, n)
  227. {
  228.         register int    size;
  229.  
  230.         if (n < 0)
  231.                 return (FALSE);
  232.         if (backchar(FALSE, 1) == FALSE)
  233.                 return (FALSE);
  234.         size = 0;
  235.         while (n--) {
  236.                 while (inword() == FALSE) {
  237.                         if (backchar(FALSE, 1) == FALSE)
  238.                                 return (FALSE);
  239.                         ++size;
  240.                 }
  241.                 while (inword() != FALSE) {
  242.                         if (backchar(FALSE, 1) == FALSE)
  243.                                 return (FALSE);
  244.                         ++size;
  245.                 }
  246.         }
  247.         if (forwchar(FALSE, 1) == FALSE)
  248.                 return (FALSE);
  249.         return (ldelete(size, TRUE));
  250. }
  251.  
  252. /*
  253.  * Return TRUE if the character at dot is a character that is considered to be
  254.  * part of a word. The word character list is hard coded. Should be setable.
  255.  */
  256. inword()
  257. {
  258.         register int    c;
  259.  
  260.         if (curwp->w_doto == llength(curwp->w_dotp))
  261.                 return (FALSE);
  262.         c = lgetc(curwp->w_dotp, curwp->w_doto);
  263.         if (c>='a' && c<='z')
  264.                 return (TRUE);
  265.         if (c>='A' && c<='Z')
  266.                 return (TRUE);
  267.         if (c>='0' && c<='9')
  268.                 return (TRUE);
  269.         if (c=='$' || c=='_')                   /* For identifiers      */
  270.                 return (TRUE);
  271.         return (FALSE);
  272. }
  273.  
  274.